package zhaoqiang.mediarecoder;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class MainActivity extends Activity {


    private ImageView imgPhoto;

    File targetFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imgPhoto = (ImageView) findViewById(R.id.image_photo);

    }

    /**
     * 获取并拍照
     */
    public void btn_Photo(View view) {
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,1);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        //TODO  应该判断返回吗
                if (data != null) {
                    Bitmap bitmap = data.getParcelableExtra("data");

                    if (bitmap!=null){
                        imgPhoto.setImageBitmap(bitmap);

                    }
        }else if(requestCode==2){
            if(resultCode == RESULT_OK){
                if(targetFile != null&& targetFile.exists()){
                    BitmapFactory.Options options=new BitmapFactory.Options();
                    options.inSampleSize=4;
                    Bitmap bitmap=BitmapFactory.decodeFile(targetFile.getAbsolutePath(),options);
                }
            }
        }

    }

    //保存文件：
    public void btn_savePhoto(View view) {
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


        getImageFile();

        //保存图片
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(targetFile));
        startActivityForResult(intent, 2);

    }

    private void getImageFile() {
        //设置 EXTRA_OUTPUT 数值来知道保存文件的位置, fromFile 图片存储的位置
        String state= Environment.getExternalStorageState();

        File dir;

        //获取内存卡状态：
        if(state.equals(Environment.MEDIA_MOUNTED)){
            //保存到dcim
            dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

            if(!dir.exists()){
                dir=Environment.getExternalStorageDirectory();
                dir=new File(dir,"MediaRecorder/Images");
                if(!dir.exists()){
                    dir.mkdirs();
                }
            }
            targetFile=new File(dir,"Image"+System.currentTimeMillis()+".jpg");
        }else{
            //内部存储区
            dir = getFilesDir();
        }
        targetFile=new File(dir,"Image"+System.currentTimeMillis()+".jpg");
    }

    //打开照片
    public void btn_openPhoto(View view) {
        Intent intent=new Intent(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent,3);
    }

    //
    public void btn_Video(View view) {
        Intent intent=new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
        startActivityForResult(intent,4);
    }



   public void  myCamera(View view){

       Intent intent=new Intent(this,CameraActivity.class);
       startActivity(intent);

   }


}